package com.wsc.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.wsc.entity.ConsumeLog;
import com.wsc.entity.ReadRoom;
import com.wsc.repository.ConsumeLogRepository;
import com.wsc.service.ConsumeLogService;

@Service
public class ConsumeLogServiceImpl implements ConsumeLogService {
	@Autowired
	private ConsumeLogRepository cr;

	@Override
	public Map<String, Object> getAll(Map<String, Object> params) {
		ConsumeLog c = (ConsumeLog) params.get("consumelog");
		Specification<ConsumeLog> spec = getSpec(c);
		Integer page = (Integer) params.get("page");
		Integer limit = (Integer) params.get("limit");
		Pageable pa = PageRequest.of(page, limit);
		// 返回实体页
		Page<ConsumeLog> res = cr.findAll(spec, pa);
		// 获取list集合数据
		List<ConsumeLog> data = res.getContent();
		// count 数据总条数
		long count = res.getTotalElements();
		if (data.size() > 0) {
			params.put("code", 0);
			params.put("data", data);
			params.put("count", count);
		} else {
			params.put("code", 1);
			params.put("msg", "数据异常");
		}
		return params;
	}

	private Specification<ConsumeLog> getSpec(ConsumeLog c) {
		return new Specification<ConsumeLog>() {
			@Override
			public Predicate toPredicate(Root<ConsumeLog> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				Predicate pre = cb.conjunction();
				List<Expression<Boolean>> sqllist = pre.getExpressions();
				// id cardno readroomid intime outtime status
				if (!StringUtils.isEmpty(c.getCardno())) {
					sqllist.add(cb.like(root.get("cardno"), "%" + c.getCardno() + "%"));
				}
				if (!StringUtils.isEmpty(c.getReadroomid())) {
					sqllist.add(cb.equal(root.get("readroomid"), c.getReadroomid()));
				}

				if (!StringUtils.isEmpty(c.getIntime()) && !StringUtils.isEmpty(c.getOuttime())) {
					SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
					Date intime = null;
					Date outtime = null;
					try {
						intime = format.parse(c.getIntime());
						outtime = format.parse(c.getOuttime());
					} catch (ParseException e) {
						intime = new Date();
						outtime = new Date();
						e.printStackTrace();
					}
					sqllist.add(cb.between(root.get("intime"), intime, outtime));
				}
				if (!StringUtils.isEmpty(c.getStatus())) {
					sqllist.add(cb.equal(root.get("status"), c.getStatus()));
				}
				return pre;
			}
		};

	}

	@Override
	public List<Object[]> getChartData(String intime, String outtime, Integer status,String reader) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");// 注意月份是MM
		if (intime.equals("")) {
			// 防止抛出ParseException异常
			intime = "2000-01-01";
		}
		if (outtime.equals("")) {
			// 防止抛出ParseException异常
			outtime = sdf.format(new Date());
		}
		//最早进入时间、最晚进入时间
		Date time1;
		Date time2;
		try {
			time1 = sdf.parse(intime);
			time2 = sdf.parse(outtime);
		} catch (ParseException e) {
			time1 = null;
			time2 = null;
			e.printStackTrace();
		}
		//统计教师、学生还是所有人员
		if("教师".equals(reader)) {
			return cr.getChartDataAboutTeacher(time1,time2,status);
		}else if("学生".equals(reader)) {
			return cr.getChartDataAboutStudent(time1,time2,status);
		}else {
			return cr.getChartData(time1, time2, status);
		}
	}

	@Override
	public void updateAllStatusToZero() {
		cr.updateAllStatusToZero();
	}
	
}
